package com.zfzs.post.service.impl;

import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.dto.DisTerminalAgentDTO;
import com.zfzs.post.model.vo.DistribuVO;
import com.zfzs.post.modules.agent.domain.Agent;
import com.zfzs.post.modules.agent.service.AgentService;
import com.zfzs.post.modules.agent.service.dto.AgentDTO;
import com.zfzs.post.modules.post.domain.PostTerminalDistribu;
import com.zfzs.post.modules.post.domain.dto.DistribuDTO;
import com.zfzs.post.modules.post.domain.dto.QueryTerminalDistribuDTO;
import com.zfzs.post.modules.post.service.PostTerminalDistribuService;
import com.zfzs.post.modules.post.service.TerminalInfoService;
import com.zfzs.post.modules.post.service.mybatis.DistribuBatisService;
import com.zfzs.post.service.DistributionSerivce;
import com.zfzs.post.service.JwtUserDetailsService;
import com.zfzs.post.service.TerminalService;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wq
 * @date 2019/10/9 14:12
 * @description
 */

@Service
public class DistributionSerivceImpl implements DistributionSerivce {

    @Autowired
    private JwtUserDetailsService jwtUserDetailsService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private PostTerminalDistribuService postTerminalDistribuService;

    @Autowired
    private DistribuBatisService distribuBatisService;

    @Autowired
    private TerminalService terminalService;

    @Override
    public List<DistribuVO> getOwnerTerminal(QueryTerminalDistribuDTO dto) {
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();

        boolean startEmFlag = StringUtils.isEmpty(dto.getStartSn());
        boolean endEmFlag = StringUtils.isEmpty(dto.getEndSn());

        if (startEmFlag != endEmFlag) {
            throw new BadRequestException("请完整填写起止Sn码");
        }

        if (!startEmFlag) {
            dto.setNumSign(StringUtils.isNumeric(dto.getStartSn()) && StringUtils.isNumeric(dto.getEndSn()) ? "Yes" : "No");
        }

        dto.setAgentId(agentInfo.getId());
        dto.setChannelId(agentInfo.getChannelId());
        List<DistribuDTO> list = distribuBatisService.getOwnerTerminal(dto);

        return CloneUtils.batchClone(list, DistribuVO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void disTerminal2Agent(DisTerminalAgentDTO disTerminalAgentDTO) {
        Long agentId = disTerminalAgentDTO.getAgentId();
        Agent agent = agentService.findById(agentId);
        AgentDTO info = jwtUserDetailsService.getAgentInfo();
        if (!agent.getPidId().equals(info.getId())) {
            throw new BadRequestException("该用户并非您的直属下级,请重新分配");
        }
        List<String> snList = disTerminalAgentDTO.getSnList();

        List<PostTerminalDistribu> list = postTerminalDistribuService.getTerDistribuListBySnList(snList);

        list=list.stream().filter(e->e.getAgentId().equals(info.getId())).collect(Collectors.toList());

        if(!list.isEmpty()){
            for (PostTerminalDistribu d : list) {
                d.setAcceptId(agentId);
                d.setState(1);
                d.setReceiveState(1);
                d.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            }

            postTerminalDistribuService.updateDisList(list);

            String[] disSnList = list.stream().map(PostTerminalDistribu::getSn).toArray(String[]::new);

            insertDistribu(agentId, info.getChannelId(), disSnList);

            terminalService.updateFinalOwner(agentId,disSnList);

        }else {
            throw new BadRequestException("当前机器不属于您,无法分配");
        }
    }

    @Override
    public void insertDistribu(Long agentId, Long channelId, String... snList) {
        for (String s : snList) {
            PostTerminalDistribu distribu = new PostTerminalDistribu();
            distribu.setSn(s);
            distribu.setAgentId(agentId);
            distribu.setCreateTime(new Timestamp(System.currentTimeMillis()));
            distribu.setState(2);
            distribu.setReceiveState(1);
            distribu.setChannelId(channelId);
            postTerminalDistribuService.create(distribu);
        }

    }
}
